import java.util.ArrayList;
import java.util.List;

public class VolleyballSystem {
    private List<Team> teams;
    private List<Match> matches;

    public VolleyballSystem() {
        this.teams = new ArrayList<>();
        this.matches = new ArrayList<>();
    }

    // 添加队伍
    public Team addTeam(String name) {
        int id = teams.size() + 1;
        Team team = new Team(id, name);
        teams.add(team);
        return team;
    }

    // 创建比赛
    public Match createMatch(Team homeTeam, Team awayTeam) {
        int id = matches.size() + 1;
        Match match = new Match(id, homeTeam, awayTeam);
        matches.add(match);
        return match;
    }

    // 记录比分
    public void recordSetScore(Match match, int setNumber, int homeScore, int awayScore) {
        Team homeTeam = match.getHomeTeam();
        Team awayTeam = match.getAwayTeam();

        // 创建局分对象
        SetScore setScore = new SetScore(setNumber, homeScore, awayScore, homeTeam, awayTeam);

        // 添加到比赛中
        match.addSetScore(setScore);
    }

    // 计算排名
    public List<Team> calculateRanking() {
        // 复制队伍列表
        List<Team> rankedTeams = new ArrayList<>(teams);

        // 按规则排序：1. 胜场数 2. 局分比 3. 小分比
        rankedTeams.sort((t1, t2) -> {
            // 比较胜场数
            int result = Integer.compare(t2.getWins(), t1.getWins());
            if (result != 0) return result;

            // 比较局分比
            result = Double.compare(t2.getSetRatio(), t1.getSetRatio());
            if (result != 0) return result;

            // 比较小分比
            return Double.compare(t2.getPointRatio(), t1.getPointRatio());
        });

        return rankedTeams;
    }

    // 打印排名表
    public void printRanking() {
        List<Team> rankedTeams = calculateRanking();

        System.out.println("\n===== 排球比赛排名表 =====");
        System.out.printf("%-5s %-10s %-5s %-5s %-8s %-8s %-10s %-10s\n",
                "排名", "队伍", "胜场", "负场", "胜局数", "负局数", "局分比", "小分比");
        for (int i = 0; i < rankedTeams.size(); i++) {
            Team team = rankedTeams.get(i);
            System.out.printf("%-5d %-10s %-5d %-5d %-8d %-8d %-10.2f %-10.2f\n",
                    (i+1),team.getName(),team.getWins(),team.getLosses(),
                    team.getSetsWon(),team.getSetsLost(),
                    team.getSetRatio(),team.getPointRatio());
        }
    }
}






















